<?php
/**
 * bs多规格sku商城系统
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站地址: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2025/03/15
 * 描述:
 */

namespace app\result\auto;

use app\model\auto\CreditLog;
use app\result\BaseResult;


class CreditLogResult extends BaseResult
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new CreditLog();
    }


    // 表格列
    public function el_table_column(): array
    {
        return [
            ['prop' => 'id', 'type' => 'text', 'label' => '日志ID', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'before_num', 'type' => 'text', 'label' => '交易前', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'num', 'type' => 'text', 'label' => '交易额', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'after_num', 'type' => 'text', 'label' => '交易后', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'remark', 'type' => 'text', 'label' => '备注', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'order_sn', 'type' => 'text', 'label' => '订单编号', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'created_at', 'type' => 'text', 'label' => '交易时间', 'minWidth' => '120', 'align' => 'center',],
        ];
    }

    //列表搜索字段
    private function _searchField(): array
    {
        return [
            ['field' => 'keyword', 'type' => 'text', 'label' => '搜索', 'placeholder' => '请输入搜索内容'],
        ];
    }

    // 获取公共条件限制
    private function _where($args = []): array
    {
        $where = [];
        $keyword = trim($args['keyword'] ?? '');
        if ($keyword) {
            $where[] = ['remark', 'like', '%' . trim($args['keyword']) . '%'];
        }


        return $where;
    }

    public function list($args = []): array
    {
        $page = intval($args['page'] ?? 1);
        $pageSize = intval($args['limit'] ?? 10);
        $sort_by = trim($args['sort_by'] ?? 'id desc');
        $where = $this->_where($args);
        $total = $this->model->data_count($where);
        $data = $this->model->data_list($where, '*', $sort_by, $page, $pageSize);
        return $this->success_return('success', [
            'total' => $total,
            'list' => $data,
            'el_table_column' => $this->el_table_column(),
            'search_fields' => $this->_searchField()
        ]);
    }

    public function get($args = []): array
    {
        $id = intval($args['id'] ?? 0);
        $data = $this->model->data_get(["id" => $id]);
        if (!$data) {
            return $this->error_return('数据不存在');
        }
        return $this->success_return('success', [
            'info' => $data
        ]);
    }


    public function api_list($user_id, $args = [])
    {
        $page = intval($args['page'] ?? 1);
        $pageSize = intval($args['limit'] ?? 50);
        $where = [];
        $where[] = ['user_id', '=', $user_id];
        if (!empty($args['keyword'])) {
            $where[] = ['remark', 'like', '%' . trim($args['keyword']) . '%'];
        }
        $data = $this->model->data_list($where, '*', 'id desc', $page, $pageSize);
        return $this->success_return('success', [
            'list' => $data,
        ]);
    }


}
